//
//  NVCSmartConfig.m
//  EspTouchDemo
//
//  Created by THNVC on 2018/5/29.
//  Copyright © 2018年 Espressif. All rights reserved.
//

#import "NVCSmartConfig.h"
// 系统
#import <SystemConfiguration/CaptiveNetwork.h>

// nvc
#import "NVCTouchTask.h"

@interface NVCSmartConfig()
@property (nonatomic,strong) NSDictionary *SSIDInfo;

@property (nonatomic,strong) NVCTouchTask *tochTask;
@end

@implementation NVCSmartConfig


// 配置smartconfig , 同步线程
-(void)smartConfigWithPwd:(NSString *)pwd {
    dispatch_async(dispatch_get_global_queue(0, 0),^{
        [self fetchNetInfo];
        NSString *wifiName = self.SSIDInfo[@"SSID"];
        NSString *bSsid    = self.SSIDInfo[@"BSSID"];
        NSLog(@"wifiname:%@",wifiName);
        
        [self.tochTask touchTaskWithSsid:wifiName AndBSsid:bSsid AndPwd:pwd AndAES:nil];

        NSArray *resultArray = [self.tochTask executeForResults:1];
        self.smartConfigResultBlock(resultArray);
    });
}

// 结束
-(void)endSmartConfig {
    [self.tochTask stopSmartConfig];
}



#pragma mark - 获取WiFi相关信息
//{
//    BSSID = "74:ea:c8:10:94:e0";
//    SSID = "nvc-online";
//    SSIDDATA = <6e76632d 6f6e6c69 6e65>;
//}
// refer to http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library
- (void)fetchNetInfo
{
    NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
    
    NSDictionary *SSIDInfo;
    for (NSString *interfaceName in interfaceNames) {
        SSIDInfo = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
        
        BOOL isNotEmpty = (SSIDInfo.count > 0);
        if (isNotEmpty) {
            break;
        }
    }
    self.SSIDInfo = SSIDInfo;
}


#pragma mark - 懒加载
-(NVCTouchTask *)tochTask {
    if(!_tochTask) {
        _tochTask = [[NVCTouchTask alloc] init];
    }
    return _tochTask;
}

@end
